<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use Exception;
use PDOException;
use think\Db;
use think\exception\ValidateException;

/**
 * 物流信息接口配置
 *
 * @icon fa fa-circle-o
 */
class Expressquery extends Backend
{

    /**
     * ExpressConfig模型对象
     * @var \app\admin\model\ExpressConfig
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\expressquery\ExpressqueryConfig;
        $this->view->assign("expressTypeList", $this->model->getExpressTypeList());
        $this->view->assign("kuaidi100TypeList", $this->model->getKuaidi100TypeList());
        $expressList = Db::name('expressquery')->where(['coding' => ['neq', '']])->select();
        $this->view->assign("expressList", $expressList);
    }

    /**
     * 查看
     */
    public function index()
    {
        $row = $this->model->limit(1)->select();
        if (!$row) {
            $params['express_type'] = 'kuaidiniao';
            $this->model->allowField(true)->save($params);
            $row = $row = $this->model->get($this->model->id);
        } else {
            $row = $row[0];
        }
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $params = $this->preExcludeFields($params);
                $result = false;
                Db::startTrans();
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
                        $row->validateFailException(true)->validate($validate);
                    }
                    $result = $row->allowField(true)->save($params);
                    Db::commit();
                } catch (ValidateException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (PDOException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    $this->success();
                } else {
                    $this->error(__('No rows were updated'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    /**
     * 测试物流查询接口
     * @param $params ['express']        物流公司，这里传进来的是express数据表中的express字段值
     * @param $params ['expresssn']      物流单号
     * @author Created by Xing <464401240@qq.com>
     */
    public function demo()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                //在需要查询物流接口的地方使用以下两行代码即可返回数据
                $expModle = new \addons\expressquery\library\Expressquery();
                $list = $expModle->getExpressList($params['express'], $params['expresssn'], $params['phone']);
                return json($list);
            }
        }
        $this->error(__('An unexpected error occurred', ''));
    }

    /**
     * 新增
     */
    public function add()
    {
        $this->error();
    }

    /**
     * 回收站
     */
    public function recyclebin()
    {
        $this->error();
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $this->error();
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        $this->error();
    }

    /**
     * 真实删除
     */
    public function destroy($ids = "")
    {
        $this->error();
    }

    /**
     * 还原
     */
    public function restore($ids = "")
    {
        $this->error();
    }

    /**
     * 批量更新
     */
    public function multi($ids = "")
    {
        $this->error();
    }

    /**
     * 导入
     */
    protected function import()
    {
        $this->error();
    }
}
